<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                 |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Controller;

/**
 * 车商企业认证
 *
 * @author Xmh
 */
use AuctionCore\CarDealer\Dealer;
use AuctionCore\CarDealer\DealerCompanyCertify;
use AuctionCore\CarDealer\DealerWaitCertify;

class CarDealerCompanyCertifyController extends BaseController
{
    public function __construct() {
        parent::__construct();
        $this->emp_id = $this->user_info['emp_id'];
        $this->DealerWaitCertify = new DealerWaitCertify();
        $this->DealerCompanyCertify = new DealerCompanyCertify();
    }

    public function index(){
        $this->display();
    }

    /**
     * 车商待认证列表
     */
    public function company_certify_list()
    {
        $page = I("get.p", 1);
        $size = 10;
        $params = array();
        $params['status'] = I('get.status/d');
        $params['dealer_id'] = I('get.dealer_id/d');
        $params['company_name'] = I('get.company_name','','trim');
        $params['business_license_num'] = I('get.business_license_num','','trim');
        $params['auth_name'] = I('get.auth_name','','trim');
        $params['auth_phone'] = I('get.auth_phone/d');
        $params['start_time'] = I('get.start_time');
        $params['end_time'] = I('get.end_time');
        //获取list
        $result = $this->DealerCompanyCertify->getCompanyCertifyList($page, $size, $params);
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show();   // 分页显示输出
        //模板赋值
        $this->assign("p", $page);
        $this->assign('page', $show);   // 赋值分页输出
        $this->assign('list', $result['list']);
        $this->assign('params', $params);
        $this->display();
    }

    /**
     * 企业认证审核
     */
    public function company_certify_aduit()
    {
        $dealer_id = I('get.id/d', 0);
        $status = I('get.status/d');
        if (empty($dealer_id) || (empty($status) || $status > 1)) {
            $this->helpJsonResult(400, "车商信息错误!");
        }
        $dealer = new Dealer($dealer_id);
        $dealer_detail = $dealer->dealer_base_info();
        if ($dealer_detail['auth_type'] != 2 || $dealer_detail['auth_status'] != 1) {
            $this->helpJsonResult(400, "车商状态错误!");
        }
        $company_certify_detail = $this->DealerCompanyCertify->getCompanyCertifyDetail($dealer_id);
        $this->assign('detail', $company_certify_detail);
        $this->assign('status', $status);
        $this->display();
    }

    /**
     * 处理企业认证审核
     */
    public function do_company_certify_aduit(){
        $dealer_id = I('post.id/d',0);
        if (empty($dealer_id)) {
            $this->helpJsonResult(400, "车商信息错误!");
        }
        $dealer = new Dealer($dealer_id);
        $dealer_detail = $dealer->dealer_base_info();
        if ($dealer_detail['auth_type'] != 2 || $dealer_detail['auth_status'] != 1) {
            $this->helpJsonResult(400, "车商状态错误!");
        }
        $certify_result = I('post.certify_result/d'); //1、通过 2、驳回
        $certify_remark = I('post.certify_remark', '', 'trim');
        if ($certify_result == 2) {   //驳回
            if (empty($certify_remark)) {
                $this->helpJsonResult(400, "请填写审核备注!");
            }
            $data['auth_status'] = 3;
            $company_certify_result = 4;
        }else{
            $certify_remark = '审核通过';
            $data['auth_status'] = 2;
            $company_certify_result = 3;
        }
        $data['auth_audit_time'] = date("Y-m-d H:i:s");
        $company_certify_log = [
            'dealer_id'=> $dealer_id,
            'emp_id' => $this->emp_id,
            'remark' => $certify_remark,
            'trace_result' => $company_certify_result,
            'trace_type' => 4
        ];
        M()->startTrans();
        $res1 = $this->DealerWaitCertify->add_certify_trace_log($company_certify_log);
        $dealer = new Dealer($dealer_id);
        $res2 = $dealer->updateDealer($data);
        if (!$res1 || $res2 === false) {
            M()->rollback();
            $this->helpJsonResult(500, "操作失败");
        }
        //提交事务
        M()->commit();
        $this->helpJsonResult(200, "操作成功");
    }

    /**
     * 企业签约跟踪
     */
    public function company_certify_track()
    {
        $dealer_id = I('get.id/d', '');
        $status = I('get.status/d');
        if (empty($dealer_id) || (empty($status) || $status > 3)) {
            $this->helpJsonResult(400, "车商信息错误!");
        }
        $dealer = new Dealer($dealer_id);
        $dealer_detail = $dealer->dealer_base_info();
        if ($dealer_detail['auth_type'] != 2 || $dealer_detail['auth_status'] != 2) {
            $this->helpJsonResult(400, "车商状态错误!");
        }
        $company_certify_detail = $this->DealerCompanyCertify->getCompanyCertifyDetail($dealer_id);
        $this->assign('detail', $company_certify_detail);
        $company_sign_trace_log = $this->DealerWaitCertify->get_certify_trace_log($dealer_id, 3);
        $this->assign('company_sign_trace_log', $company_sign_trace_log);
        $company_certify_trace_log = $this->DealerWaitCertify->get_certify_trace_log($dealer_id, 4);
        $this->assign('company_certify_trace_log', $company_certify_trace_log);
        $this->assign('status', $status);
        $this->display();
    }

    /**
     * 处理企业签约跟踪
     */
    public function do_company_sign_track()
    {
        $dealer_id = I('post.id/d');
        if (empty($dealer_id)) {
            $this->helpJsonResult(400, "车商信息错误!");
        }
        $dealer = new Dealer($dealer_id);
        $dealer_detail = $dealer->dealer_base_info();
        if ($dealer_detail['auth_type'] != 2 || $dealer_detail['auth_status'] != 2) {
            $this->helpJsonResult(400, "车商状态错误!");
        }
        $trace_result = I('post.trace_result/d'); //1、继续跟踪 2、不再跟踪(跟踪失败)
        $trace_remark = I('post.trace_remark', '', 'trim');
        $sign_log['dealer_id'] = $dealer_id;
        $sign_log['sign_result'] = $trace_result;
        $next_trace_time = I('post.next_trace_time', '');
        if ($trace_result == 1) {   //继续跟踪
            if (empty($next_trace_time)) {
                $this->helpJsonResult(400, "请选择下次跟踪时间!");
            }
            $sign_log['next_trace_time'] = $next_trace_time;
            $data['sign_trace_status'] = 1;
        }else{
            $sign_log['sign_failed_remark'] = $trace_remark;
            $data['sign_trace_status'] = 2;
        }
        $certify_trace_log = [
            'dealer_id' => $dealer_id,
            'emp_id' => $this->emp_id,
            'remark' => $trace_remark,
            'trace_result' => $trace_result,
            'next_trace_time'=> $next_trace_time,
            'trace_type' => 3
        ];
        M()->startTrans();
        $res1 = $this->DealerWaitCertify->add_certify_trace_log($certify_trace_log);
        $res2 = $this->DealerWaitCertify->add_dealer_sign_log($dealer_id, $sign_log, 2);
        $dealer = new Dealer($dealer_id);
        $res3 = $dealer->updateDealer($data);
        if (!$res1 || !$res2 || $res3 === false) {
            M()->rollback();
            $this->helpJsonResult(500, "操作失败");
        }
        //提交事务
        M()->commit();
        $this->helpJsonResult(200, "操作成功");
    }


    /**
     * 企业签约跟踪
     */
    public function company_certify_view()
    {
        $dealer_id = I('get.id/d', '');
        $status = I('get.status/d');
        if (empty($dealer_id) || (empty($status) || $status < 4)) {
            $this->helpJsonResult(400, "车商信息错误!");
        }
        $company_certify_detail = $this->DealerCompanyCertify->getCompanyCertifyDetail($dealer_id);
        $this->assign('detail', $company_certify_detail);
        $company_sign_trace_log = $this->DealerWaitCertify->get_certify_trace_log($dealer_id, 3);
        $this->assign('company_sign_trace_log', $company_sign_trace_log);
        $company_certify_trace_log = $this->DealerWaitCertify->get_certify_trace_log($dealer_id, 4);
        $this->assign('company_certify_trace_log', $company_certify_trace_log);
        $this->assign('status', $status);
        $this->display();
    }

    /**
     * 车商企业认证签约合同
     */
    public function company_contract()
    {
        $dealer_id = I('get.id/d', '');
        if (empty($dealer_id)) {
            $this->helpJsonResult(400, "车商信息错误!");
        }
        $this->display();
    }
}